from email.mime.text import MIMEText
import random
import smtplib
from connecttodatabase.connect_to_database import E_User
from flask import jsonify
from werkzeug.security import generate_password_hash, check_password_hash
from sqlalchemy.exc import SQLAlchemyError

class Ctr_User_S:

    @staticmethod
    def register(user_id, email):
        vetify = E_User.vetify_email(user_id,email)
        if vetify:
            smtp_server = 'smtp.qq.com'
            smtp_port = 587
            smtp_username = '485484849@qq.com'
            smtp_password = 'wwwyiqiqjcwicbaf'
            code = str(random.randint(100000, 999999))
            msg = MIMEText(f'Your verification code is: {code}')
            msg['Subject'] = 'Verification Code'
            msg['From'] = '485484849@qq.com'
            msg['To'] = email

            server = smtplib.SMTP(smtp_server, smtp_port)
            server.starttls()
            server.login(smtp_username, smtp_password)
            server.send_message(msg)
            server.quit()
            return code
        return None
    
    @staticmethod
    def add_user(user_id,password,name,email):
        return E_User.add_user(user_id,password,name,email)
    
    @staticmethod
    def delete_user(user_id):
        return E_User.delete_user(user_id)
    
    @staticmethod
    def email_id_exist(user_id, email):
        return E_User.vetify_email(user_id,email)
    
    @staticmethod
    def login(user_id, password):
        user = E_User.get_user_by_id(user_id=user_id)
        if not user or  user.get('user_password') != password:
            return None
        return 1
    
    @staticmethod
    def get_all_user():
        return E_User.get_user()
   